# 将label和像素数据分离
import os
import random

import cv2
import numpy as np
import pandas as pd

# # 修改为train.csv在本地的相对或绝对地址
file ='/mnt/d/datasets/face_data/train.csv'
out_folder = '/mnt/d/datasets/face_data/48x48'
img_folder="images"
label_folder="labels"
train_percent = 0.7
def split_train():
    return random.random()< train_percent


def create_folder(file):
    f= os.path.dirname(file)
    print(f)
    if os.path.exists(f):
        print("已存在")
        return
    os.makedirs(f)

def create_train(folder,stem,img,label):
    fld= f'{out_folder}/{folder}/{img_folder}/{stem}.jpg'
    print("fld",fld)
    create_folder(fld)
    cv2.imwrite(fld, img)  # 写图片
    fld= f'{out_folder}/{folder}/{label_folder}/{stem}.txt'
    create_folder(fld)
    with open(fld, mode='w+') as f:
        f.write(create_label(label))

def create_label(lb):
    return f'{lb} 0.0 0.0 1.0 1.0'


# 读取数据
# df = pd.read_csv(file)
# 提取label数据
# df_y = df[['label']]
# 提取feature（即像素）数据
features = np.loadtxt('data.csv')
labels = np.loadtxt('label.csv')
# 将label写入label.csv
# df_y.to_csv('label.csv', index=False, header=False)
# # 将feature数据写入data.csv
# df_x.to_csv('data.csv', index=False, header=False)
# labels = df_y.items()
for i in range(features.shape[0]):
    face_array = features[i, :].reshape((48, 48))  # reshape
    label =labels[i]
    if split_train():
      create_train("train",i,face_array,label)
    else:
      create_train("test", i, face_array, label)


#
# import cv2
# import numpy as np
#
# # 指定存放图片的路径
# cls = ['生气', '厌恶', '恐惧', '高兴', '难过', '惊讶', '中立']
# path = './face'
# # 读取像素数据
# data = np.loadtxt('data.csv')
#
# # 按行取数据
# # for i in range(data.shape[0]):
# for i in range(12):
#     print(data[i,:].shape)
#     print(data[i, :10])
#     face_array = data[i, :].reshape((48, 48))  # reshape
#     # file = f'{path}/{i}.jpg'
#     # cv2.imwrite(file, face_array)  # 写图片
#     # print(path)
